using Microsoft.Extensions.Configuration;
using Model.Core.Entity;
using System.Text.Json;

namespace Model.Core.Services
{
    public class LocalRobotInfo
    {
        private readonly IConfiguration _configuration;
        private readonly string _configFilePath;

        public LocalRobotInfo(IConfiguration configuration)
        {
            _configuration = configuration;
            _configFilePath = Path.Combine(AppContext.BaseDirectory, "Configures", "robotbase.json");
        }

        public string RobotId => _configuration.GetSection("RobotInfo:RobotId").Value;
        public string RobotName => _configuration.GetSection("RobotInfo:RobotName").Value;
        public string RobotType => _configuration.GetSection("RobotInfo:RobotType").Value;
        public string MapId => _configuration.GetSection("RobotInfo:MapId").Value;
        public string MapName => _configuration.GetSection("RobotInfo:MapName").Value;

        public void UpdateRobotInfo(RobotInfo robot)
        {
            var robotInfo = new
            {
                RobotInfo = new
                {
                    RobotId = robot.RobotId,
                    RobotName = robot.Name,
                    RobotType = robot.RobotType,
                    MapId = robot.MapInfo.Id,
                    MapName = robot.MapInfo.Name
                }
            };

            var json = JsonSerializer.Serialize(robotInfo, new JsonSerializerOptions { WriteIndented = true });
            File.WriteAllText(_configFilePath, json);
            Task.Delay(1000).Wait();
            //   GetRobotInfoJson();
        }

        public string GetRobotInfoJson()
        {
            return JsonSerializer.Serialize(new
            {
                RobotId = RobotId,
                RobotName = RobotName,
                RobotType = RobotType,
                MapId = MapId,
                MapName = MapName
            });
        }
    }
}